package com.itheima.behaviour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.behaviour.dto.ReadBehaviorDto;
import com.itheima.behaviour.feign.ApBehaviourEntryFeign;
import com.itheima.behaviour.mapper.ApReadBehaviorMapper;
import com.itheima.behaviour.pojo.ApBehaviorEntry;
import com.itheima.behaviour.pojo.ApReadBehavior;
import com.itheima.behaviour.service.ApReadBehaviorService;
import com.itheima.common.pojo.Result;
import com.itheima.common.util.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * APP阅读行为表 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2021-08-30
 */
@Service
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements ApReadBehaviorService {

    @Autowired
    private ApReadBehaviorMapper apReadBehaviorMapper;

    @Autowired
    private ApBehaviourEntryFeign apBehaviourEntryFeign;

    /**
     * 添加用户阅读行为
     * @param dto
     */
    @Override
    public void readBehavior(ReadBehaviorDto dto) {
        //1:查询操作用户的信息（游客、系统会员）
        Integer userId = RequestContext.getNodeValue("id");
        Result<ApBehaviorEntry> entryResult = apBehaviourEntryFeign.getByEquipmentIdOrUserId(dto.getEquipmentId(), userId);
        ApBehaviorEntry entry = entryResult.getData();
        if(entry!=null){
            //2:查询出当前用户对该文章的阅读行为
            QueryWrapper<ApReadBehavior> queryWrapper = new QueryWrapper<ApReadBehavior>();
            queryWrapper.eq("entry_id",entry.getId());
            queryWrapper.eq("article_id",dto.getArticleId());
            ApReadBehavior apReadBehavior = apReadBehaviorMapper.selectOne(queryWrapper);

            //3:如果没有该文章的阅读行为，往数据库中添加记录
            if(apReadBehavior==null){
                apReadBehavior = new ApReadBehavior();
                apReadBehavior.setEntryId(entry.getId());
                apReadBehavior.setArticleId(dto.getArticleId());
                apReadBehavior.setCount(1);
                apReadBehavior.setReadDuration(dto.getReadDuration());
                apReadBehavior.setPercentage(dto.getPercentage());
                apReadBehavior.setLoadDuration(dto.getLoadDuration());
                apReadBehavior.setCreatedTime(LocalDateTime.now());
                apReadBehavior.setUpdatedTime(LocalDateTime.now());
                apReadBehaviorMapper.insert(apReadBehavior);
            }else{
                //4:如果存在该文章的阅读行为，则更新
                apReadBehavior.setCount(apReadBehavior.getCount()+1);
                apReadBehavior.setReadDuration(apReadBehavior.getReadDuration()+dto.getReadDuration());
                apReadBehavior.setPercentage(dto.getPercentage());
                apReadBehavior.setLoadDuration(apReadBehavior.getLoadDuration()+dto.getLoadDuration());
                apReadBehavior.setCreatedTime(LocalDateTime.now());
                apReadBehavior.setUpdatedTime(LocalDateTime.now());
                apReadBehaviorMapper.updateById(apReadBehavior);
            }
        }
    }
}
